package com.project.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.project.bean.LogBean;
import com.project.bean.UserBean;
import com.project.service.ILogService;
import com.project.service.IRoleService;
import com.project.service.IUserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.cache.Cache;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("user")
public class UserController {
    @Autowired
    private IUserService service;
    @Autowired
    private IRoleService roleService;
    @Autowired
    private ILogService logService;
    @Autowired
    private AuthorizingRealm customRealm;

    /**
     * 通过用户名密码登录，如果登陆成功，则记录登陆日志，同时需要查询该用户拥有的权限集合
     */
    @RequestMapping("login")
    public UserBean login(String userName, String pwd, HttpServletRequest request) {
        //得到主体
        Subject subject = SecurityUtils.getSubject();
        try {
            Cache<Object, AuthenticationInfo> authenticationInfoCache=customRealm.getAuthenticationCache();
            if (authenticationInfoCache!=null){
                authenticationInfoCache.remove(userName);
            }
            subject.login(new UsernamePasswordToken(userName, pwd));
        } catch (UnknownAccountException ua){
            System.out.println("用户名错误");
            return null;
        }catch (IncorrectCredentialsException ice){
            System.out.println("密码错误");
            return null;
        }
        UserBean loginUser = (UserBean) subject.getPrincipal();

        //登陆日志
        logService.add(new LogBean(loginUser.getRealName()));
        //绑定共享数据
        request.setAttribute("loginUser", loginUser);
        return loginUser;
    }


    @RequiresPermissions("user:findByItem")
    @RequestMapping("findByItem")
    public IPage<UserBean> findByItem(Integer pageNO, String name) {
        return service.findByItem(pageNO, name);
    }


    @RequiresPermissions("user:findByName")
    @RequestMapping("findByName")
    public UserBean findByName(String name) {
        return service.findByName(name);
    }


    @RequiresPermissions("user:add")
    @RequestMapping("add")
    public String add(UserBean user, Integer... roleIdArray) {
        if (service.findByName(user.getName()) == null) {
            service.add(user, roleIdArray);
            return "ok";
        } else {
            return "no";
        }
    }

    @RequiresPermissions("user:del")
    @RequestMapping("del")
    public String del(Integer userId) {
        service.del(userId);
        return "ok";
    }


    @RequiresPermissions("user:findById")
    @RequestMapping("findById")
    public UserBean findById(Integer id) {
        return service.findById(id);
    }

    @RequiresPermissions("user:updateRole")
    @RequestMapping("updateRole")
    public String updateRole(Integer userId, Integer... newRoleArray) {
        service.updateRole(userId, newRoleArray);
        return "ok";
    }

    @RequestMapping("updatePwd")
    public String updatePwd(Integer userId, String newPwd) {
        service.updatePwd(userId, newPwd);
        return "ok";
    }
}
